/*
 * Copyright 2011 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.ritesdk;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

/**
 * The Facade class with the main method.
 * 
 * @author Hideki Shima
 *
 */
public class MainClass extends AbstractMainClass {

  @Override
  protected Options createOptions() {
    Options options = new Options();
    options.addOption("h", "help", false, "print this message and exit");
    options.addOption("t", "task", true, "task name (BC, MC, EXAM or RITE4QA)");
    options.addOption("i", "input", true, "input file");
//    options.addOption("f", "factory", true, "system factory class name (optional)");
    options.addOption("s", "system", true, "system class name");
    return options;
  }

  @Override
  public void run(CommandLine cmd, Task task) throws Exception {
    long t0 = System.nanoTime();
    String inputPath = cmd.getOptionValue("i"); // e.g. "data/bc_training.txt"
//    String factoryClassName = cmd.getOptionValue("f"); // e.g. "edu.cmu.lti.ritesdk.sample.SampleMultiEngineFactory"
    String systemClassName = cmd.getOptionValue("s"); // e.g. "edu.cmu.lti.ritesdk.sample.RandomRecognizerMC"

    File inputFile  = new File(inputPath);
    File outputFile = new File("target/rite-result.txt");
    
    DataSet data = FileUtils.readRiteXml(inputFile);
    
    // We may introduce an Abstract Factory in the future.
    IRiteFactory factory = new RiteSystemFactory(systemClassName);
    AbstractRiteSystem r = factory.create();
    AnalyzedDataSet analyzedData = r.run(data, task);
    FileUtils.writeRiteResultToFile(analyzedData, outputFile);
    
    long t1 = System.nanoTime();
    double t = (double)(t1-t0)/(double)1000000000;
    System.out.println("\n-------------------------------------------------------");
    System.out.println("Saved run result to:\n"+outputFile.getAbsolutePath()+"\n");
    System.out.println("Done running system on "+data.size()
            +" pairs in "+nf.format(t)+" sec.");
    System.out.println("-------------------------------------------------------\n");
    
    File evalOutputFile = new File("target/eval_"+outputFile.getName());
    EvaluationResult evalResult = Evaluator.evaluate(analyzedData, task);
    System.out.println(evalResult.getEvaluationSummary());
    FileUtils.writeStringToFile(evalResult.getEvaluationDetail(), evalOutputFile);
  }
  
  public static void main(String[] args) {
    new MainClass().run(args);
  }

}
